//
//  CitySelect.m
//  BSLCar
//
//  Created by dlios on 15/5/25.
//  Copyright (c) 2015年 lyz. All rights reserved.
//

#import "CitySelect.h"

@implementation CitySelect

- (void)createDBTable {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    NSString *dbPath = [path stringByAppendingPathComponent:@"CityInfo.db"];
//  NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"CityInfo.db" ofType:nil];
    self.db = [FMDatabase databaseWithPath:dbPath];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createDBTable];
    }
    return self;
}

- (NSArray *)selectAreaData {
    NSMutableArray *array = [NSMutableArray array];
    NSString *selSql = @"select * from AreaDB";
    if ([self.db open]) {
        FMResultSet *set = [self.db executeQuery:selSql];
        while ([set next]) {
            AreaDBModel *model = [AreaDBModel getAreaDBModel];
            model.areaId = [[set objectForColumnName:@"areaId"] stringValue];
            model.areaName = [set objectForColumnName:@"areaName"];
            model.cityId = [[set objectForColumnName:@"cityId"] stringValue];
            [array addObject:model];
        }
    }
    [self.db close];
    return array;
}

- (NSArray *)selectCityData {
    NSMutableArray *array = [NSMutableArray array];
    NSString *selSql = @"select * from CityDB";
    if ([self.db open]) {
        FMResultSet *set = [self.db executeQuery:selSql];
        while ([set next]) {
            CityDBModel *model = [CityDBModel getCityDBModel];
            model.proId = [[set objectForColumnName:@"proId"] stringValue];
            model.cityName = [set objectForColumnName:@"cityName"];
            model.cityId = [[set objectForColumnName:@"cityId"] stringValue];
            model.letter = [set objectForColumnName:@"letter"];
            [array addObject:model];
        }
    }
    [self.db close];
    return array;
}

- (NSArray *)selectProvinceData {
    NSMutableArray *array = [NSMutableArray array];
    NSString *selSql = @"select * from ProvinceDB";
    if ([self.db open]) {
        FMResultSet *set = [self.db executeQuery:selSql];
        while ([set next]) {
            ProvinceDBModel *model = [ProvinceDBModel getProvinceDBModel];
            model.proid = [[set objectForColumnName:@"proId"] stringValue];
            model.letter = [set objectForColumnName:@"letter"];
            model.proName = [set objectForColumnName:@"proName"];
            [array addObject:model];
        }
    }
    [self.db close];
    return array;
}

@end
